/**
 * Created by Phantom on 30/07/14.
 */

/**
 * Created by Phantom on 30/07/14.
 * Fait la vérification du formulaire d'inscription
 */

appEstim.controller('inscriptionCtrl',function($scope,$http) {
    $scope.content="dfd";
    $scope.formInfo = {};
    $scope.saveData = function() {

        var animationIn ='animated flipInX';
        var animationOut ='animated flipOutX';

        if (!$scope.login) {
            $scope.loginRequired = 'login requis';
            $('#logError').removeClass(animationOut);
            $('#logError').addClass(animationIn);
        }else{
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationOut);
            $scope.loginRequired = '';
        }
        if (!$scope.mail) {
            $scope.mailRequired = 'mail requis';
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationIn);
        }else{
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationOut);
            $scope.mailRequired = '';
        }
        if (!$scope.password) {
            $scope.passwordRequired = 'mot de passe requis';
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationIn);
        }else{
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationOut);
            $scope.passwordRequired = '';
        }
        if(!$scope.passwordConf){
            $scope.passwordConfRequired = 'mot de passse de confirmation requis';
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationIn);
        }else{
            $('#logError').removeClass(animationIn);
            $('#logError').addClass(animationOut);
            $scope.passwordConfRequired ='';
        }

        if($scope.password!=$scope.passwordConf){
            $scope.error = 'les mots de passe sont différents';
        }
        // AJAX !!!!!
        if($scope.login && $scope.mail && $scope.password && $scope.passwordConf){
            // l'utilisateur existe ?
//http://devzone.co.in/angularjs-simple-example-post-data-php-page/
            $http.post('index.php?modules=managerUser&action=userExist',
                {'login': $scope.login}
             ).success(function(data) {
                    if (data)
                    {
                        if(data==1){
                            $scope.error = "existe";
                        }
                        else{
                            $scope.error= "n'existe pas";
                        }
                    }
                }).error(function(data) {
                    //gestion erreur
                });
        }
    };
});